/**
 * 待办事项状态模块
 */
import api from '@/api'

const state = {
  // 待办事项列表
  todoList: [],
  // 分页信息
  pagination: {
    total: 0,
    pageSize: 10,
    currentPage: 1
  },
  // 查询参数
  queryParams: {
    type: '',
    status: '',
    keyword: '',
    isRead: ''
  },
  // 待办事项详情
  currentTodo: null,
  // 统计信息
  stats: null,
  // 加载状态
  loading: false
}

const mutations = {
  // 设置待办事项列表
  SET_TODO_LIST: (state, { list, pagination }) => {
    state.todoList = list
    state.pagination = pagination || state.pagination
  },
  // 设置查询参数
  SET_QUERY_PARAMS: (state, params) => {
    state.queryParams = { ...state.queryParams, ...params }
  },
  // 设置当前待办事项
  SET_CURRENT_TODO: (state, todo) => {
    state.currentTodo = todo
  },
  // 设置统计信息
  SET_STATS: (state, stats) => {
    state.stats = stats
  },
  // 设置加载状态
  SET_LOADING: (state, loading) => {
    state.loading = loading
  },
  // 更新待办事项状态
  UPDATE_TODO_STATUS: (state, { id, status }) => {
    const index = state.todoList.findIndex(item => item.id === id)
    if (index !== -1) {
      state.todoList[index].status = status
    }
    
    if (state.currentTodo && state.currentTodo.id === id) {
      state.currentTodo.status = status
    }
  },
  // 标记待办事项为已读
  MARK_TODO_AS_READ: (state, id) => {
    const index = state.todoList.findIndex(item => item.id === id)
    if (index !== -1) {
      state.todoList[index].isRead = true
    }
    
    if (state.currentTodo && state.currentTodo.id === id) {
      state.currentTodo.isRead = true
    }
  }
}

const actions = {
  // 获取待办事项列表
  getTodoList({ commit, state }) {
    commit('SET_LOADING', true)
    
    // 构造查询参数
    const params = {
      ...state.queryParams,
      pageSize: state.pagination.pageSize,
      currentPage: state.pagination.currentPage
    }
    
    // 调用API
    return api.todo.getTodoList(params)
      .then(response => {
        commit('SET_TODO_LIST', response)
        return response
      })
      .finally(() => {
        commit('SET_LOADING', false)
      })
  },
  
  // 获取待办事项详情
  getTodoDetail({ commit }, id) {
    commit('SET_LOADING', true)
    
    return api.todo.getTodoDetail(id)
      .then(response => {
        commit('SET_CURRENT_TODO', response)
        // 标记为已读
        commit('MARK_TODO_AS_READ', id)
        api.todo.markTodoAsRead(id)
        return response
      })
      .finally(() => {
        commit('SET_LOADING', false)
      })
  },
  
  // 获取待办事项统计
  getTodoStats({ commit }) {
    return api.todo.getTodoStats()
      .then(response => {
        commit('SET_STATS', response)
        return response
      })
  },
  
  // 更新查询参数
  updateQueryParams({ commit, dispatch }, params) {
    commit('SET_QUERY_PARAMS', params)
    // 重置页码
    commit('SET_TODO_LIST', { list: [], pagination: { ...state.pagination, currentPage: 1 } })
    // 重新获取列表
    return dispatch('getTodoList')
  },
  
  // 更改分页
  changePage({ commit, dispatch }, page) {
    commit('SET_TODO_LIST', { 
      list: [], 
      pagination: { ...state.pagination, currentPage: page } 
    })
    return dispatch('getTodoList')
  },
  
  // 审批待办事项
  approveTodo({ commit }, { id, data }) {
    commit('SET_LOADING', true)
    
    return api.todo.approveTodo(id, data)
      .then(() => {
        commit('UPDATE_TODO_STATUS', { id, status: { id: '3', name: '已完成', color: 'green' } })
        return { success: true }
      })
      .finally(() => {
        commit('SET_LOADING', false)
      })
  },
  
  // 拒绝待办事项
  rejectTodo({ commit }, { id, data }) {
    commit('SET_LOADING', true)
    
    return api.todo.rejectTodo(id, data)
      .then(() => {
        commit('UPDATE_TODO_STATUS', { id, status: { id: '4', name: '已拒绝', color: 'red' } })
        return { success: true }
      })
      .finally(() => {
        commit('SET_LOADING', false)
      })
  },
  
  // 标记为已读
  markAsRead({ commit }, id) {
    return api.todo.markTodoAsRead(id)
      .then(() => {
        commit('MARK_TODO_AS_READ', id)
        return { success: true }
      })
  }
}

const getters = {
  // 获取待处理的待办事项数量
  pendingCount: (state) => {
    return state.stats ? state.stats.pending : 0
  },
  // 获取未读的待办事项数量
  unreadCount: (state) => {
    return state.stats ? state.stats.unread : 0
  },
  // 获取所有待办事项数量
  totalCount: (state) => {
    return state.stats ? state.stats.total : 0
  },
  // 获取按类型分组的待办数量
  todoCountByType: (state) => {
    return state.stats ? state.stats.byType : []
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
} 